'use strict';

const _ = require('lodash');

advertController.$inject = ['advertService'];

function advertController(advertService) {
  var vm = this;
  vm.untreatedadvertlist = [];
  vm.processedlist = [];
  vm.onputlist = [];

  vm.untreat = function () {
    advertService.getBystatus("0").then(function (res) {
      vm.untreatedadvertlist = res.data;
    }).catch(err => console.log(err))
  }

  vm.onput = function () {
    advertService.getonordown().then(function (res) {
      vm.onputlist = res.data;
    }).catch(err => console.log(err))
  }

  vm.processe = function () {
    advertService.getByNotstatus("0").then(function (res) {
      vm.processedlist = res.data;
    }).catch(err => console.log(err))
  }

  vm.update = function (type, status, index) {
    if (_.isEqual(type, "0")) {
      let advert = vm.untreatedadvertlist[index];
      advert.status = status;
      advertService.updateStatus(advert).then(function (res) {
        vm.untreatedadvertlist.splice(index, 1);
      }).catch(err => console.log(err))
    } else if (_.isEqual(type, "1")) {
      let advert = vm.onputlist[index];
      advert.status = status;
      advert.reviewTime = new Date();
      advertService.updateStatus(advert).then(function (res) {
      }).catch(err => console.log(err))
    }

  }

  vm.untreat();
  vm.processe();
  vm.onput();
}

module.exports = advertController;